package org.hswebframework.example.crud.service;

import java.util.List;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;

public class SumAggregator<T> implements Aggregator<T> {
    private BinaryOperator accumulater;
    private Object identity;
//    private List<T> listvals;
    private Function collectf;

    public SumAggregator(Object identity, BinaryOperator accumulater,Function collectf) {
        this.accumulater = accumulater;
        this.identity = identity;
//        this.listvals = listvals;
        this.collectf = collectf;
    }

    @Override
    public Object aggregate(Object key, List<T> values) {
        List<Object> toLst = (List<Object>) values.stream().map(collectf).collect(Collectors.toList());
        return toLst.stream().reduce(identity, accumulater);
    }

}

